package com.yksj.healthtalk.ui.doctorstation;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.UserListAdapter;
import com.yksj.healthtalk.adapter.UserListAdapter.ItemHeaderOnClickListener;
import com.yksj.healthtalk.adapter.UserListAdapter.ItemOnClickListener;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.DoctorPatient;
import com.yksj.healthtalk.entity.DoctorPatientListEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.PersonInfoUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 服务中的患者
 * 
 * @author Administrator
 * 
 */
public class DoctorPatientListOption extends Fragment implements
		OnItemClickListener, OnRefreshListener<ListView>, OnClickListener, OnCheckedChangeListener,ItemOnClickListener,ItemHeaderOnClickListener{

	private ListView mListView;
	private UserListAdapter madapter;
	private final int pageSize = 1;
	private PullToRefreshListView refreshListView;
	private TextView titleTextV;
	private final String date = "0";
	private final String serviceTypeId = "0";
	private final String mGroupId = "0";
	private DoctorPatientListEntity entity;
	private PopupWindow mPopBottom;
	private View vPopView;
	private List<DoctorPatient> mlList=new ArrayList<DoctorPatient>();;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.doctor_service_fragment_listview,null);
		initView(view);
		return view;
	}

	private void initView(View view){
		view.findViewById(R.id.title_back).setOnClickListener(this);
		titleTextV = (TextView) view.findViewById(R.id.title_lable);
		titleTextV.setOnClickListener(this);
		Drawable drawable = getActivity().getResources().getDrawable(R.drawable.title_middle_triangle);
		titleTextV.setCompoundDrawables(null,null,drawable , null);
		refreshListView = (PullToRefreshListView) view.findViewById(R.id.listview_fragment);
		entity = (DoctorPatientListEntity) getArguments().getSerializable("parmar");
		titleTextV.setText(entity.getName());
		mListView = refreshListView.getRefreshableView();
		mListView.addFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.list_line_layout, null));
		madapter = new UserListAdapter(getActivity(),this,this,180);//Integer.valueOf(entity.getIndex())
		mListView.setAdapter(madapter);
		mListView.setOnItemClickListener(this);
		refreshListView.setOnRefreshListener(this);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
	}
	
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode!=Activity.RESULT_OK)return ;
		switch (requestCode) {
		case 5000:
			mlList.clear();
			initData();
			break;
		}
	}

	/**
	 * 0-已开始服务 1-已预约服务 2-已取消预约服务 3-服务历史 4-未支付患者 5-我关注的患者 6-关注我的患者
	 */

	private void initData() {
		// loginUserId type,
		HttpRestClient.doHttpDoctorOption(SmartFoxClient.getLoginUserId(),entity.getId(),"20", String.valueOf(pageSize), date,
				serviceTypeId, mGroupId, new ObjectHttpResponseHandler(getActivity()) {
					@Override
					public Object onParseResponse(String content) {
						mlList.addAll(DoctorPatient.parseToList(content));
						return mlList;
					}
					@Override
					public void onSuccess(Object response) {
						super.onSuccess(response);
						madapter.updateData((List<DoctorPatient>)response);
					};
					@Override
					public void onFinish() {
						super.onFinish();
						refreshListView.onRefreshComplete();
					}
				}
		);
	}

	
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		try {
			CustomerInfoEntity entity =new CustomerInfoEntity();
			DoctorPatient patient = mlList.get(position-1);
			entity.setId(patient.getCustomerId());
			entity.setName("".equals(patient.getRemarksName())?patient.getCustomerNickname():patient.getRemarksName());
			FriendHttpUtil.onItemClick(getActivity(), entity);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		refreshListView.onRefreshComplete();
	}


	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if(mPopBottom!=null&&mPopBottom.isShowing())mPopBottom.dismiss();
		RadioButton mButton= (RadioButton) vPopView.findViewById(checkedId);
		ToastUtil.showShort(getActivity(), mButton.getText().toString());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			getActivity().setResult(Activity.RESULT_OK,getActivity().getIntent());
			getActivity().finish();
			break;
		}
	}

	@Override
	public void onRightClick(int positon, View view, Object object) {
		DoctorPatient mPatient=(DoctorPatient) object;
		Intent intent;
		if(mPatient.getServiceStatusCode().equals("175")||mPatient.getServiceStatusCode().equals("180")){
			intent=new Intent(getActivity(),DoctorServiceStatusContent.class);
			intent.putExtra("ORIDERID", mPatient.getOrderId());
			intent.putExtra("CUSTOMER_ID", mPatient.getCustomerId());
			intent.putExtra("DOCTORID", SmartFoxClient.getLoginUserId());
			intent.putExtra("tag", 1000);
			startActivityForResult(intent,5000);
		}else if("1".equals(mPatient.getServiceTypeId())){//普通
			intent=new Intent(getActivity(),DoctorServiceStatusContent.class);
			intent.putExtra("entity", mPatient);
			intent.putExtra("tag", 2000);
			startActivityForResult(intent,5000);
			
		}else if("2".equals(mPatient.getServiceTypeId())||"3".equals(mPatient.getServiceTypeId())||"50".equals(mPatient.getServiceStatusCode())){//预约
			intent=new Intent(getActivity(),DoctorServiceStatusContent.class);
			intent.putExtra("entity", mPatient);
			intent.putExtra("tag", 3000);
			startActivityForResult(intent,5000);
		}
	}

	@Override
	public void onHeaderClick(int positon, View view, Object object) {
		DoctorPatient mPatient=(DoctorPatient) object;
		PersonInfoUtil.choiceActivity(mPatient.getCustomerId(), getActivity(), mPatient.getRoleId());
	}

	
}
